﻿package level4.grid_case.astar
{
	import level4.grid_case.grid.Node;

	/**
	 * 寻路估价的几种方式
	 * */
	public class PathFormula
	{
		//曼哈顿,速度快些
		public static const MANHATTEN:String 	= "manhattan";
		//欧几里德几何评估,自然些
		public static const EUCLIDIAN:String 	= "euclidian";
		//对角线,效率最高
		public static const DIAGONAL:String 	= "diagonal";
		//对角线的代价
		private static const diagCost:Number	= Math.SQRT2;
		
		public function PathFormula(){}
		/**
		计算路径的消耗量
		1 寻路估价方式
		2 当前节点
		3 终点节点
		4 直线的消耗量
		*/
		public static function calculatePath(pathMethod:String,curNode:Node,endNode:Node,straightCost:Number=1.0):Number{
			if(pathMethod == MANHATTEN){
				return manhattan(curNode,endNode,straightCost);
			}else if(pathMethod == EUCLIDIAN){
				return euclidian(curNode,endNode,straightCost);
			}else if(pathMethod == DIAGONAL){
				return diagonal(curNode,endNode,straightCost);
			}
			return 0;
		}
		//曼哈顿 寻路估价
		public static function manhattan(curNode:Node,endNode:Node,straightCost:Number=1.0):Number{
			return Math.abs(curNode.x - endNode.x) * straightCost + Math.abs(curNode.y-endNode.y)*straightCost;
		}
		//欧几里德 寻路估价
		public static function euclidian(curNode:Node,endNode:Node,straightCost:Number=1):Number{
			var dx:Number = curNode.x - endNode.x;
			var dy:Number = curNode.x - endNode.y;
			return Math.sqrt(dx*dx+dy*dy) * straightCost;
		}
		//对角线 寻路估价
		public static function diagonal(curNode:Node,endNode:Node,straightCost:Number=1):Number{
			var dx:Number = Math.abs(curNode.x - endNode.x);
			var dy:Number = Math.abs(curNode.y - endNode.y);
			var diag:Number = Math.min(dx,dy);
			var straight:Number = dx + dy;
			return diagCost * diag + straightCost *(straight-2*diag);
		}
	}
}